package com.wanghua.mall.order.listener;

import com.rabbitmq.client.Channel;
import com.wanghua.common.dto.mq.SecKillOrderDto;
import com.wanghua.mall.order.config.OrderRabbitMQConfig;
import com.wanghua.mall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 监听秒杀订单
 *
 * @author wanghua
 * @create 2021-09-11 21:38
 */
@RabbitListener(queues = OrderRabbitMQConfig.ORDER_SECKILL_ORDER_BINDING_NAME)
@RestController
public class OrderSecKillListener {

    @Resource
    private OrderService orderService;

    @RabbitHandler
    public void handleSecKillOrder(Message message, SecKillOrderDto secKillOrderDto, Channel channel) throws IOException {
        try {
            orderService.createSecKillOrder(secKillOrderDto);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
